package com.newbingo.action;

import java.util.Date;

import com.newbingo.dto.AgentDto;
import com.newbingo.dto.MemberDto;
import com.newbingo.dto.OperatorDto;
import com.newbingo.dto.imple.IOperatorDto;
import com.newbingo.entity.Agent;
import com.newbingo.entity.Operator;
import com.newbingo.service.imple.IOperatorService;
import com.newbingo.util.Pager;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.googlecode.jsonplugin.annotations.JSON;

public class OperatorAction extends ActionSupport {
	
	//登录传参
	private String uname;
	private String upwd;

	private static final long serialVersionUID = 1L;

	private IOperatorService operatorService = null;

	private Pager pager = null;
	private OperatorDto dto = null;
	private Operator operator = new Operator();

	/**
	 * 分查询参数
	 */
	private int start;
	private int limit = 15;
	
	//修改状态传值
	private Integer id;
	private String status;

	private boolean success = false;

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	/** *************操作方法************* */
	
	@Override
	public String execute() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		OperatorDto operator = operatorService.findByLogin(uname, upwd);	    
		if (operator != null) {
			session.setAttribute("member", operator);
			return "success";
		} else {
			return "fail";
		}
		
	}

	

	/*
	 * 查询当前代理下的操作员
	 */
	public String findOperator() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		AgentDto agentDto = (AgentDto) session.getAttribute("user");
		this.setPager(operatorService.findOperator(start, limit, agentDto
				.getUno()));
		success = true;
		return SUCCESS;
	}

	/*
	 * 新增
	 */
	public String create() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		AgentDto agentDto = (AgentDto) session.getAttribute("user");
		Agent agent=new Agent();
		agent.setUno(agentDto.getUno());
		operator.setAgent(agent);
		operator.setRegTime(new Date());
		operator.setStatus("冻结");
		operatorService.save(operator);
		success = true;
		// return SUCCESS;
		return "create";
	}

	/*
	 * 修改
	 */
	public String update() {
		operatorService.merge(operator);
		success = true;
		// return SUCCESS;
		return "update";
	}
	
	/*
	 * 修改状态
	 */
	public String updateStatus(){
		Operator operator=operatorService.findByOperatorId(id);
		operator.setStatus(status);
		operatorService.merge(operator);
		success=true;
		return SUCCESS;
	}

	/*
	 * 删除
	 */
	public String delete() {
		operatorService.delete(operator);
		success = true;
		// return SUCCESS;
		return "delete";
	}

	/*
	 * 根据ID查询
	 */
	public String findById() {
		this.setDto(operatorService.findById(operator.getUno()));
		success = true;
		// return SUCCESS;
		return "find";
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	@JSON(serialize = false)
	public IOperatorService getOperatorService() {
		return operatorService;
	}

	public void setOperatorService(IOperatorService operatorService) {
		this.operatorService = operatorService;
	}

	@JSON(serialize = false)
	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public OperatorDto getDto() {
		return dto;
	}

	public void setDto(OperatorDto dto) {
		this.dto = dto;
	}

	public Operator getOperator() {
		return operator;
	}

	public void setOperator(Operator operator) {
		this.operator = operator;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

}
